package top.tenebrous.yzl.im.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import top.tenebrous.yzl.im.entity.ImWebSocketProperties;
import top.tenebrous.yzl.im.interceptor.WebSocketInterceptor;
import top.tenebrous.yzl.im.server.WebSocketChatHandlerServer;
import top.tenebrous.yzl.im.server.WebSocketHandlerServer;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private WebSocketChatHandlerServer webSocketChatHandler;

    @Autowired
    private WebSocketHandlerServer webSocketServerHandler;

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;

    @Qualifier("webSocketProperties")
    @Autowired
    private ImWebSocketProperties imWebSocketProperties;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //配置拦截器、处理器，设置跨域
        registry.addHandler(webSocketChatHandler, imWebSocketProperties.getChatSuffixPath())
                .addHandler(webSocketServerHandler, imWebSocketProperties.getServerSuffixPath())
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
    }
}